//------------------------------------------------- 
//Hungnq 
//Media-Music ver 1.0
//front-end script 
//20/06/2008
//-------------------------------------------------
//--Construct domready for Mootools framework
window.addEvent('domready', function(){
    $$('a').each(function(link_obj){
        if(link_obj.retrieve('added') != true){         
            var caller = link_obj.get('rev');
            var area = link_obj.get('rel');
            var url = link_obj.get('href');
            //alert(url);
            if(caller!=null && area !=null){
                link_obj.store('added', true);   
                link_obj.addEvent('click', function(e) {
                e = new Event(e).stop();
                if(caller=='removeItem'){
                    if(confirm('Bạn không muốn nghe bài này nữa?')==true){
                       (link_obj.getParent('.plistrow')).dispose();
                       handleRemovePlit(url); 
                       removeItem('mPlayer',area);
                    }   
                }
                else if(caller=='index' || caller=='song' || caller=='singer' || caller== 'playlist' || caller=='user')
                {
                    //alert('oke');
                    $$('li').each(function(m)
                    {
                    if(m.hasClass('menu')){
                       m.removeClass('act'); 
                       m.addClass('normal');
                    }
                    });
                    var emenu = link_obj.getParent('.menu');
                    emenu.removeClass('normal');
                    emenu.addClass('act');
                     handleCaller(url,area); 
                }
                else if(caller=='search'){
                    var this_form = link_obj.getParent('.fs');
                     var this_request = new Request({
                        method: 'post',
                        url: this_form.get('action'),
                        data: this_form,
                        onSuccess: function(response_text){
                            $(area).innerHTML = '';
                            $(area).innerHTML = response_text;
                        },
                    }).send();
                
               /* $$('input').each(function(m)
                    {
                    var t;
                    if(m.hasClass('txtSearch')){
    
                        var varTxt = m.value;
                        if(t!=null){
                        urlS = url + '&key=' + varTxt + '&t=' + t;
                        }
                        else
                        {
                         urlS = url + '&key=' + varTxt;
                        }
                        alert (urlS);
                        handleCaller(urlS,area);
                    }
                    });      */
                        
                       
                   
                }
                else
                { 
                    handleCaller(url,area);     
                }
                
             }); 
            }
            else if(caller!=null && area ==null)
            {   
                link_obj.store('added', true);
                link_obj.addEvent('click', function(e) { 
                e = new Event(e).stop(); 
                handleRemovePlit(url);
                });
            }  
        }
         
    });
    //Call function control link
    var handleCaller = function(url,area){
        var request = new Request({
            url: url,
            onSuccess : function(data){
            $(area).innerHTML = '';
            $(area).innerHTML = data;
            request = null; 
            reAddEvent();
        }
        }).get();    
    };
    var handleRemovePlit = function(url){
        var request = new Request({
            url: url
        }).get();    
    };
    var reAddEvent = function(){
    window.addEvent('domready', function(){
    $$('a').each(function(link_obj){
         if(link_obj.retrieve('added') != true){         
            var caller = link_obj.get('rev');
            var area = link_obj.get('rel');
            var url = link_obj.get('href');
            //alert(url);
            if(caller!=null && area !=null){
                link_obj.store('added', true);   
                link_obj.addEvent('click', function(e) {
                e = new Event(e).stop();
                if(caller=='removeItem'){
                    if(confirm('Bạn không muốn nghe bài này nữa?')==true){
                       (link_obj.getParent('.plistrow')).dispose();
                       handleRemovePlit(url); 
                       removeItem('mPlayer',area);
                    }   
                }
                else if(caller=='removeAll'){
                
                }
                else
                { 
                    handleCaller(url,area);     
                }
                
             }); 
            }
            else if(caller!=null && area ==null)
            {   
                link_obj.store('added', true);
                link_obj.addEvent('click', function(e) { 
                e = new Event(e).stop(); 
                handleRemovePlit(url);
                });
            }  
        }
         
    });
    });
    };
}); 

